﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Windows.Browser;
using System.Collections.ObjectModel;
using SalaryChannel.ViewService;
namespace SalaryChannel
{
    public delegate void BindBrushEventHandler(List<VW_PhotoPath> brushes);

    

    public class ScrollEngine
    {
        /// <summary>
        /// The time controls the left move
        /// </summary>
        private System.Windows.Threading.DispatcherTimer dtLeft;

        /// <summary>
        /// The time controls the right move
        /// </summary>
        private System.Windows.Threading.DispatcherTimer dtRight;

        /// <summary>
        /// Scroll speed of the scroll view
        /// </summary>
        public double ScrollSpeed
        {
            get;
            set;
        }

        public bool Ready
        {
            get;
            set;
        }

        public double PanelWidth
        {
            get;
            set;
        }

        public int MaxLength
        {
            get;
            set;
        }


        public BindBrushEventHandler BindBrush
        {
            get;
            set;
        }

        /// <summary>
        /// User defined scroller
        /// </summary>
        private ScrollViewer navigateScroll;

        /// <summary>
        /// Datalist to show 
        /// </summary>
        private ListBox dataList;

        /// <summary>
        /// Signal of existing default selection
        /// </summary>
        private bool existDefaultSelect;
        public ScrollEngine(ScrollViewer navigateScroll, ListBox dataList,bool existDefaultSelect)
        {
            Ready = true;
            this.navigateScroll = navigateScroll;
            this.dataList = dataList;
            this.existDefaultSelect = existDefaultSelect;
            MaxLength = 4;

            // Timer for animation of left scrolling
            dtLeft = new System.Windows.Threading.DispatcherTimer();
            dtLeft.Interval = new TimeSpan(0, 0, 0, 0, 50);
            dtLeft.Tick += new EventHandler(dtLeft_Tick);

            // Timer for animation of right scrolling
            dtRight = new System.Windows.Threading.DispatcherTimer();
            dtRight.Interval = new TimeSpan(0, 0, 0, 0, 50);
            dtRight.Tick += new EventHandler(dtRight_Tick);
        }

        /// <summary>
        /// Bind the current images to the list.
        /// </summary>
        /// <param name="d"></param>
        private void BindCurrent(Direction direction)
        {

            List<VW_PhotoPath> currentPics = new List<VW_PhotoPath>();
            for (int i = 0; i < MaxLength; i++)
            {
                // If click "Previous" button, it binds the first four photo
                if (direction == Direction.Previous)
                {
                    if (!string.IsNullOrEmpty(((VW_PhotoPath)dataList.Items[i]).PhotoID))
                    {
                        currentPics.Add((VW_PhotoPath)(dataList.Items[i]));
                    }
                }
                // If click "Next" button, it binds the last four photo
                else
                {
                    if (i + MaxLength < dataList.Items.Count)
                    {
                        currentPics.Add((VW_PhotoPath)(dataList.Items[i + 4]));
                    }
                }
            }

            navigateScroll.ScrollToHorizontalOffset(0.0);
            
            dataList.ItemsSource = currentPics;

            if (currentPics.Count > 0)
            {
                if (existDefaultSelect)
                {
                    dataList.SelectedIndex = 0;
                }
            }

            if (BindBrush != null)
            {
                BindBrush(currentPics);
            }

        }

        /// <summary>
        /// Combine the current photos and additional photos together.
        /// </summary>
        /// <param name="l"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        private List<VW_PhotoPath> CombinePics(List<VW_PhotoPath> left, List<VW_PhotoPath> right)
        {
            List<VW_PhotoPath> sum = new List<VW_PhotoPath>();
            
            for (int i = 0; i < left.Count; i++)
            {
                sum.Add(left[i]);
            }
            
            // Fill the empty position
            if (left.Count < MaxLength)
            {
                for (int i = 0; i < MaxLength - left.Count; i++)
                {
                    VW_PhotoPath photo = new VW_PhotoPath();
                    photo.Path = (new Uri(HtmlPage.Document.DocumentUri, new Uri(@"Image/empty.jpg", UriKind.Relative))).ToString();
                    sum.Add(photo);
                }
            }

            for (int i = 0; i < right.Count; i++)
            {
                sum.Add(right[i]);
            }
            return sum;
        }



        /// <summary>
        /// Timer for scrolling to previous photos.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dtLeft_Tick(object sender, EventArgs e)
        {
            this.navigateScroll.ScrollToHorizontalOffset(this.navigateScroll.HorizontalOffset - ScrollSpeed);

            if (this.navigateScroll.HorizontalOffset <= 0.0)
            {
                dtLeft.Stop();
                BindCurrent(Direction.Previous);
                lock (this)
                {
                    dataList.SelectedIndex = 0;
                    Ready = true;
                }
            }
        }

        /// <summary>
        /// Timer for scrolling to next four photos.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dtRight_Tick(object sender, EventArgs e)
        {
            this.navigateScroll.ScrollToHorizontalOffset(this.navigateScroll.HorizontalOffset + ScrollSpeed);

            if (this.navigateScroll.HorizontalOffset >= PanelWidth)
            {
                dtRight.Stop();
                BindCurrent(Direction.Next);
                lock (this)
                {
                    Ready = true;
                    dataList.SelectedIndex = 0;
                }
            }
        }

        /// <summary>
        /// Scroll the photos horizontally.
        /// </summary>
        /// <param name="direction"></param>
        /// <param name="addPhoto"></param>
        public void ScrollToHorizontal(Direction direction, List<VW_PhotoPath> addPhoto)
        {
            lock (this)
            {
                Ready = false;
            }

            List<VW_PhotoPath> currentPhoto = new List<VW_PhotoPath>();
            for (int i = 0; i < dataList.Items.Count; i++)
            {
                currentPhoto.Add((VW_PhotoPath)(dataList.Items[i]));
            }

            if (direction == Direction.Previous)
            {
                // Combine the current and additional photos
                dataList.ItemsSource = CombinePics(addPhoto, currentPhoto);

                navigateScroll.ScrollToHorizontalOffset(PanelWidth);
                dtLeft.Start();
            }
            else
            {
                dataList.ItemsSource = CombinePics(currentPhoto, addPhoto);

                navigateScroll.ScrollToHorizontalOffset(0.0);
                dtRight.Start();
            }
        }
    }
}
